<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of home
 *
 * @author b4by
 */
class home extends MY_Controller {

	function __construct() {
		parent::__construct();
		$this->data['is_home'] = true;
	}

	public function index() {
		$this->data['homecats'] = $this->getHomeCats();
		$this->data['slide'] = $this->getSlideImages();
		$this->data['albums'] = $this->albums();
		$main_cat = $this->model_catalogue->get_info_rule(array('main'=> 1));
		if(!$main_cat){
			$temp = $this->model_catalogue->get_list($this->user_lang->id,array(
				'where'	=> array('active'	=> 1),
				'limit'	=> array(0,1)
			));
			$main_cat=$temp[0];
		}
		$main_cat = $this->model_catalogue->get_info($main_cat->id);
		$main_cat->pages = $this->getCatNews($main_cat->id);
		$main_page = $this->model_page->get_info_rule(array('main'	=> 1));
		if(!$main_page){
			$temp = $this->model_page->get_list(array(
				'limit'	=> array(0,1)
			));
			$main_page=$temp[0];
		}
		$main_page = $this->model_page->get_info($main_page->id);
		$this->data['main_cat'] = $main_cat;
		$this->data['main_page']= $main_page;
		$this->data['temp'] = 'home/index';
		$this->load_view();
	}

	public function albums() {
		$page = request('p', 1);
		$this->load->model('model_product');
		$pagination = pagination_init($this->model_product->get_total(array('home'	=> 1)), $page, 6);
		$albums = $this->model_product->get_list($this->user_lang->id, array(
			'where'	=> array('home'	=> 1),
			'limit' => array($pagination->from, $pagination->limit),
			'order'	=> array('index','asc')
				));
		foreach ($albums as &$album) {
			$album->link = url_create('site', 'ViewProduct', 'index', $album->id, $album->name);
			$album->thumb = image_url($album->thumb);
		}
		$this->data['pagination'] = $pagination;
		if ($this->ajax) {
			echo json_encode(array(
				'state' => 'success',
				'data' => $albums,
				'pagination' => $pagination
			));
		}
//		pr($pagination);die;
		return $albums;
	}

	private function getCatNews($cat_id)
	{
		$main_news = $this->model_page->get_list(array(
			'where'	=> array(
				'c.cat_id'	=> $cat_id,
				'home'	=> 1,
				),
			'order'	=> array('p.id','desc'),
			
			'limit'	=> array(0,8),
		));
		return $main_news;
	}
	
	private function getHomeCats() {

		//if (!$homecats = $this->cache->file->get('homecats')) {
			$this->load->model('model_catalogue');
			$this->load->model('model_product');
			$this->load->model('model_page');
			$homecats = $this->model_catalogue->get_list($this->user_lang->id, array(
				'where' => array(
					'active' => 1,
					'home' => 1,
					'type'	=> 0,
				),
				'order' => array('index', 'asc')
					));

			foreach ($homecats as $cat) {
				if ($cat->type == 0)
					$cat->pages = $this->model_page->get_list(array(
						'where' => array(
							'cat_id' => $cat->id,
							'home' => 1
						),
						'limit' => array(0, 9)
							));
				else if ($cat->type == 1)
					$cat->products = $this->model_product->get_list($this->user_lang->id,array(
						'where' => array(
							'cat_id' => $cat->id,
							'home' => 1
						),
						'limit' => array(0, 9)
							), $this->location, $cat->id);
			}
			//$this->cache->file->save('homecats', $homecats, 60);
		//}
//		pr($homecats);die;
		return $homecats;
	}

}

?>
